package com.ncu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.ncu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2016102600761420";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDfeU6biEx4oy3kcTDBjPsTglU0Euxe/QORfmItdHCW0E4nz/xytYT99mLlCRQBDXOvI07gPPa8tQVY3tk2B0IbYjep7zI6kQoBOwQOxkkAT7IebFWK5FBai5c6hNo15gva01v21wZVhjjQzCjCvYwTy3X0qXhPnErzxt8M22VjQXRQ9tuC1oq9vSzAJkCow+LkEx+9IfDhZR4UNeAtZtF3Df4mW5BpwtWcUHYKCPiOgk7LnAbUzbZV0Le8p0Qol0Ln3Ww4VbEMqVJcwMkaDRmR5v/3hTtyfRf70Tl/9I0s33dyd1ah0aM0MpxgEXGiQTJdAtkh6IdKtLv69a6uDkv3AgMBAAECggEAMk18BEaJ8B3LFsDj2K2DVsDfrPkVe8w4IWfxcYnkTg5ViWCn0qpJZWEwoZ2JgzMj9yhbeN5AK/oTWg3PlvnLR560KOxOwiAjwGMoZqr7g7ZswaxhFQKPk9IYGHQPKlK+GGO7L7T3GAk+gMFz62H/RfVmd8wt6L2loXoCe0JncA/AEOvmdnsPIjjzpWxHEfUPf3kgFO3nrGxRunxndFm8MnD7U/wS4CBiC5BiCs3k/Mpy9OoVUS+9qhEQGVcuVLawo1D4j094m6MCObnaYdvXVd2rZA7A8XSq3XxwEFWTW8/kINwGRd1Lqz1tKYD7KiEBbJJLIySOvPETKlDCBu2lAQKBgQDveJoTQALRrYRjCznXUQuQrUGA4BwAJ0xfFAuwCu1BPxV67lATZQNJjR4B6HmDhcah+QXpJZJbOdjei1IcxnAUEQWtVVYHptVbiCS3p1JJLp325JF+JUQnY2EpOphhuuotvXgI+WjIzLZnfHl5u9iRZqqAq/zYSoSusFHvYLPFewKBgQDu5gmidVWRrubG9Vde0oL8VuCfLL7rRbBhCM343iaIycouuZUWWe62DlJs/tq0tbkuONDNPvh13y7tAOTY4K156SPlMtMZYOf+GzPgGX5nIEh1VJSxouvQoKQHKg1APtAVN35Yuzp1MFnosNCHiKCD2a6QLObuR9OXFu1j0c1EtQKBgQDgNGsU4NaC19FeJdGGvVN1pjDw52Jv5ltDnXH/5YsSjmyfBuaj8ews/dl9cdnrAOHjHlKhYdLwZG2elpRPap7eur009vOwAEw8AXBXHmoVKHcvA/sQPvXbZtk+D6VBww2ju4PZ6yFm9vtEUq8tAWmXYKb8B4qYOtSU9XIRC9nkZwKBgAFinBOxEnnxxazJp2QJXHESH+NMYx8CHG+zmV4kC50nbmCu+spSYyvzEU9IewQpOZWnBq7p+o7UaHU/WBRZ2Senzml5zfeLtEyo47gc7bqzWTg08VfGVg+znr/b4o/IzXQSCk2BaGldimfq/oqKpEOfc3+qgvxGtldd7qpxgPsxAoGAcjIMwE8WM7smLQVPEX/7ey+TVAxvtoITFs+NyMdrAmBHM4zgAUs+gDETpNyt9x3MQWz5Iy3XRsfy0AhrJsMeQrQNJ/aWxv3vyrI7f2k7BZcYXGq8V08BI3fwxOwjF1HU6PiPKzfbuyWwaLYEDn9MH/rkHc3EzDqO1vOEpcBHdVE=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAljiCINUnTkGGE70Yo6MP/N47AJOtPD4mga3cfDmZZzGg5jV1muKvUpGfEHDzJ6aTOvPnksTnd9vFJP7T6R4vvpXSSacfRICn0B6+Owt7V6/uMWENkLE/D4ve8DuO/uiEcqr/7BIiAIB2Yx16pj+06aiKzsjcfrAYDr25WOuwRohLMru8SJALaL0pcYVgZtdAU8y1cOP0SUG07+YVU6bzR8z1yES2Ncb0Vz7LCrTKg25CcVTcxgocVu5n9uiCyK9pb1e3J+AFEK8q24d+NluEt9DVhALwp3ZGWHIPSz5wtS7VluzfiTlwvDB8fvj9ZxPdlnGMpvOfcFQCj+0HJHqxrwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://外网地址/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\"" + timeout + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
